<?php

require_once './library/Zend/Db/Table/Abstract.php';

//require_once './library/Table.php';

class TableUtilisateurs extends Zend_Db_Table
{

    protected $_name;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;

    protected function _setup()
    {
        $this->_name = 'utilisateurs';
        $this->_primary = 'IdUtilisateur';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    public function GetNbUtilisateurs()
    {
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');

        $db = $registry->get('adapter');
        $sql = 'SELECT COUNT(*) FROM Utilisateurs';
        $result = $db->fetchAll($sql);

        return $result;
    }

    public function getUserSession($pName, $pPass)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('NomUtilisateur = ?', $pName);
        $this->_select->where('MdpUtilisateur = ?', $pPass);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result))
            return $result;
        else
            return false;
    }

    public function getUser($pName)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('NomUtilisateur = ?', $pName);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result))
            return $result;
        else
            return false;
    }

    public function updateUser($pId, $pName, $pPwd, $pMail, $pDroit, $pLangue)
    {
        $val = array('NomUtilisateur' => $pName,
            'IdModelevisuel' => '0',
            'IdDroit' => $pDroit,
            'Mail' => $pMail,
            'Langue' => $pLangue);
        if (!empty($pPwd))
            $val['mdputilisateur'] = $pPwd;
        $this->_db->update($this->_name, $val, 'IdUtilisateur = ' . $pId);
    }

    public function getIdUser($pId)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('IdUtilisateur = ?', $pId);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result))
            return $result;
    }

    public function getLstUser()
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function newUser($pName, $pPwd, $pDroit, $pMail, $pLangue)
    {
        $val = array('NomUtilisateur' => $pName,
            'MdpUtilisateur' => $pPwd,
            'IdModeleVisuel' => '0',
            'IdDroit' => $pDroit,
            'Mail' => $pMail,
            'Langue' => $pLangue);
        $this->_db->insert($this->_name, $val);
        $id = $this->_db->lastInsertId($this->_name);
        return $id;
    }

    public function delUser($pName)
    {
        $this->delete($this->_db->quoteInto('NomUtilisateur = ?', $pName));
    }

}

?>